# 不使用列表推导
example_list_01 = []
for i in range(10):
    if i % 2 == 0:
       example_list_01.append(i)

# 列表推导
example_list_02 = [i for i in range(10) if i % 2 == 0]

#使用filter也能够完成上述的功能，但是可读性并不强
example_list_03 = list(filter(lambda i: i % 2 == 0, range(10)))

#用列表推导表达式尝试计算笛卡尔积并与for循环完成的相同的功能做对比
colors = ['红色','蓝色','绿色']
clothes = ['上衣','裤子','运动鞋']

clothes_list_01 = []
for color in colors:
    for clothe in clothes:
        clothes_list_01.append((color,clothe))

clothes_list_02 = [(color,clothe) for color in colors for clothe in clothes]


#还可以将自己的函数或者python内置函数直接对生成的数组进行处理
def deal(num):
    return '处理过的' + str(num)

deal_list = [deal(i) for i in range(10)]


#通过组合两个现有列表来创建列表列表时
nums = [1, 2, 3, 4, 5]
letters = ['A', 'B', 'C', 'D', 'E']
nums_letters = [[n, l] for n in nums for l in letters]

if __name__ == "__main__":
    print(example_list_01) #[0, 2, 4, 6, 8]
    print(example_list_02)
    print(example_list_03)
    print('未使用列表推导：', clothes_list_01)
    print('使用列表推导：',clothes_list_02)
    print(deal_list )
    print(nums_letters) #[[1, 'A'], [1, 'B'], [1, 'C'], [1, 'D'], [1, 'E'], [2, 'A'], [2, 'B'], [2, 'C'], [2, 'D'], [2, 'E'], [3, 'A'], [3, 'B'], [3, 'C'], [3, 'D'], [3, 'E'], [4, 'A'], [4, 'B'], [4, 'C'], [4, 'D'], [4, 'E'], [5, 'A'], [5, 'B'], [5, 'C'], [5, 'D'], [5, 'E']]
